home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 837 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.1 KB  |  93 lines

  1. Path: engnews1.Eng.Sun.COM!usenet
  2. From: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: initialization of nonlocal static objects
  5. Date: 25 Mar 1996 16:16:43 GMT
  6. Organization: ?
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <9603251201.AA24987@lts.sel.alcatel.de>
  9. References: <4j1tm6$bmu@senator-bedfellow.MIT.EDU>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. In-Reply-To: jgealow@mtl.mit.edu's message of 24 Mar 96 04:40:44 GMT
  12. Content-Length: 3311
  13. X-Lines: 71
  14.  
  15. In article <4j1tm6$bmu@senator-bedfellow.MIT.EDU> jgealow@mtl.mit.edu
  16. (Jeffrey C. Gealow) writes:
  17.  
  18. |> I've encountered trouble with the initialization of nonlocal static
  19. |> objects.  The following parts of the ARM are relevant:
  20.  
  21. |>   ARM 3.4
  22. |>   The initialization of nonlocal static objects in a translation unit is
  23. |>   done before the first use of any function or object defined in that
  24. |>   translation unit.  Such initializations may be done before the first
  25. |>   statement of main() or deferred to any point in time before the first
  26. |>   use of a function or object defined in that translation unit.  The
  27. |>   default initialization of all static objects to zero is performed
  28. |>   before any dynamic (that is, run-time) initialization.  No further
  29. |>   order is imposed on the initialization of objects from different
  30. |>   translation units.
  31.  
  32. |>   ARM 3.4 annotation
  33. |>   A useful technique for insuring that a global object is initialized
  34. |>   only once and before its first use is to maintain a count of the
  35. |>   number of translation units using it.
  36.  
  37. |>   ARM 7.1.6
  38. |>   A const object may be initialized, but its value may not be changed 
  39. |>   thereafter.
  40.  
  41. |> Are the following observations correct?
  42.  
  43. |>   The technique described in the ARM 3.4 annotation cannot be applied to
  44. |>   const objects since the values of the objects may not be changed.
  45.  
  46. No.  Const has nothing to do with the question, since objects only
  47. become const after construction.
  48.  
  49. |>   The ARM 3.4 guarantee that the "initialization of nonlocal static
  50. |>   objects in a translation unit is done before the first use of any
  51. |>   function or object defined in that translation unit" is not accurate.
  52. |>   Functions or objects defined in a translation unit may be used in the
  53. |>   initialization of nonlocal static objects defined in other translation
  54. |>   units.  Therefore, mutual dependencies may make it impossible to honor 
  55. |>   the guarantee.
  56.  
  57. Correct.
  58.  
  59. |>   The C++ standard could be revised to include a guarantee that whenever
  60. |>   possible, the initialization of nonlocal static objects in a translation 
  61. |>   unit is done before any function or object in that translation unit is 
  62. |>   used to initialize objects in other translation units.  Otherwise, 
  63. |>   const objects should not be used.
  64.  
  65. Somehow, I don't think that these exact words can be used.
  66.  
  67. I believe that the intent of the current draft is that rule in ARM 3.4
  68. hold only for uses invoked directly or indirectly from main, but not
  69. for uses which result from the construction of other static objects.
  70.  
  71. |> The Sun SPARCompiler C++ 4.1 and G++ 2.7.2 don't even honor the ARM
  72. |> guarantee when there are no mutual dependencies.
  73.  
  74. And nor do any other compilers I'm aware of.  In practice, it is best
  75. to assume that there are no guarantees whatsoever between compilation
  76. units.  (On the other hand, it is generally safe to assume that all
  77. initialization will take place before main, at least if you are not
  78. using DLL's.  This is not guaranteed by the draft, however.)
  79.  
  80. --
  81. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  82. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  83. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  84.                 -- A la recherche d'une activitΘ dans une region francophone
  85.  
  86.  
  87.  
  88. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  89. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  90. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  91. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  92. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  93.